#!/usr/bin/env node

const cmd = require('commander');

const path = require('path');

const fs = require('fs');

const inquirer = require('inquirer');

const axios = require('axios')

// 注册命令

const option = [
    {
        type: 'input', // 交互类型
        message: '请输入账号', // 描述信息
        name: 'username'
    },
    {
        type: 'password',
        message: '请输入密码',
        name: 'password'
    }, {
        type: 'input',
        message: '请输入您的真实姓名',
        name: 'name',
        default: '小史'
    },
    {
        type: 'editor',
        message: '请输入您的个性签名',
        name: 'signature'
    },
    {
        type: 'checkbox',
        message: '请选择您的爱好',
        name: 'hobby',
        choices: ['吃饭',"睡觉","抽烟","喝酒","烫头", "打豆豆"]
    },
    {
        type: 'checkbox',
        message: "请选择您的技术栈",
        name: 'art',
        choices: [
            {
                name: 'JAVA',
                value: 'java'
            },
            {
                name: 'DIV',
                value: 'div'
            },
            {
                name: "REACT",
                value: 'React'
            },
            {
                name: 'VUE',
                value: 'Vue'
            }
        ]
    },
    {
        type: 'confirm',
        message: '是否为纯爷们',
        name: 'flag',
    },
    {
        type: 'list',
        message: '性别',
        name: 'sex',
        choices: ["男", "女", "未知"]
    }, 
    {
        type: 'expand',
        message: '请输入您的星座',
        name: 'st',
        choices: [
            {
                key: 's',
                name: "射手",
                value: '射手'
            },
            {
                key: 'b',
                name: "白羊",
                value: '白羊'
            },
            {
                key: 'z',
                name: "双子",
                value: '双子'
            },
            {
                key: 'y',
                name: "双鱼",
                value: '双鱼'
            },
            {
                key: 'v',
                name: "处女",
                value: '处女'
            },
            {
                key: 'g',
                name: "狮子",
                value: '狮子'
            },
            {
                key: 'm',
                name: "摩羯",
                value: '摩羯'
            },
            {
                key: 'p',
                name: "水瓶",
                value: '水瓶'
            },
            {
                key: 't',
                name: "天秤",
                value: '天秤'
            },
            {
                key: 'n',
                name: "金牛",
                value: '金牛'
            },
            {
                key: 'x',
                name: "天蝎",
                value: '天蝎'
            },
            {
                key: 'j',
                name: "巨蟹",
                value: '巨蟹'
            }
        ]
    }
]

const sexPage = {
    "男": "先生",
    "女": "女士",
    "未知": "用户"
}

cmd.command('getUser')
.action(() => {
    console.log('准备搜集用户信息')
    inquirer.prompt(option).then(res => {
        console.log(res)
        // res:搜集到的用户信息
        axios({
            url: `https://api.tianapi.com/xingzuo/index?key=08a1c6fe294532ba28be85a2815710ba&me=${encodeURIComponent(res.st)}`
        }).then(reslove => {
            console.log(`尊敬的${res.name}${sexPage[res.sex]}, 您的爱好有：${res.hobby.join("、")}, 您的技术栈：${res.art.join("、")}，您的星座：${res.st}, 最近的运势：${reslove.data.newslist[0].content}`)
        })
    })
})


const V = () => {
     // 读取文件
     const file = fs.readFileSync(path.join(__dirname, 'package.json')).toString();

     const obj = JSON.parse(file)

     return obj.version
 
}

// 编译参数
// version: 默认参数 -V
cmd.version(V())
cmd.parse(process.argv)